﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using Warlock.Input;

namespace Warlock
{
    class PlayerManager
    {
        static Player[] PlayerManage = new Player[20];
        static int CurrentPlayer = 0;
        static int[] freeslots = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
        
        public static int AddPlayer(Player newPlayer)
        {
            int id = 0;
            int counter = 0;
            while(counter < 20)
            {
                if (freeslots[counter] != 0)
                {
                    id = freeslots[counter];
                    freeslots[counter] = 0;
                    break;
                }
                counter++;
            }
            PlayerManage[id-1] = newPlayer;
            CurrentPlayer++;
            return id;
        }
        public static void RemovePlayer(int id)
        {
            
            PlayerManage[id] = new Player();
            freeslots[id-1] = id;
            CurrentPlayer--;
        }
        public static void update()
        {
            int counter = 0;
            while (counter < CurrentPlayer)
            {
                PlayerManage[counter].PlayerDraw();
                counter++;
            }
        }

    }
    class Player
    {
        Texture2D Playertext;
        SpriteBatch PlayerBatch;
        public Vector2 CurentPos;
        public int Healh;
        public float Rotation;
        public int Id;
        public Player()
        {
            
        }

        public Player(String Textur,Vector2 Starpos, int HP,GraphicsDevice gdevice)
        {
            PlayerBatch = new SpriteBatch(gdevice);
            Playertext = Texture2D.FromFile(gdevice, Textur);
            CurentPos = Starpos;
            Healh = HP;
            Id = PlayerManager.AddPlayer(this);
        }
        public void PlayerDraw()
        {
            PlayerBatch.Begin();
            PlayerBatch.Draw(Playertext, CurentPos, null, Color.White, Rotation, new Vector2(32,32),1f, SpriteEffects.None, 0);
            PlayerBatch.End();
        }
        public void UpdateRotation(Vector2 endPos)
        {
            Vector2 startPos = this.CurentPos;
            endPos.X = endPos.X - startPos.X;
            endPos.Y = endPos.Y - startPos.Y;
            Rotation = MathHelper.ToRadians((float)(Math.Atan2(endPos.Y, endPos.X) / (Math.PI / 180)));
        }
        public void UpdateMovement(Keys upKey, Keys DownKey, Keys leftKey, Keys rightKey)
        {
            if (Keyboard.GetState().IsKeyDown(upKey))
            {
                CurentPos += Vector2.Multiply(new Vector2((float)Math.Cos(Rotation), (float)Math.Sin(Rotation)), 1);

            }
            if (Keyboard.GetState().IsKeyDown(DownKey))
            {
                CurentPos -= Vector2.Multiply(new Vector2((float)Math.Cos(Rotation), (float)Math.Sin(Rotation)), 1);
            }
            if (Keyboard.GetState().IsKeyDown(leftKey))
            {
                CurentPos -= Vector2.Multiply(new Vector2((float)Math.Cos(Rotation + 1.5f), (float)Math.Sin(Rotation + 1.5f)), 1);
            }
            if (Keyboard.GetState().IsKeyDown(rightKey))
            {
                CurentPos += Vector2.Multiply(new Vector2((float)Math.Cos(Rotation + 1.5f), (float)Math.Sin(Rotation + 1.5f)), 1);
            }
        }
    }

}
